import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();//
        int[] hash1 = new int[26];
        for (int i = 0; i < p.length(); i++) {
            hash1[p.charAt(i) - 'a']++;
        }
        int[] hash2 = new int[26];
        int left = 0, right = 0;
        int m = p.length();
        for (; right < s.length(); right++) {
                hash2[s.charAt(right) - 'a']++;
            if (right - left + 1 >= m){
                if (check(hash1, hash2)) {
                    list.add(left);
                }
                hash2[s.charAt(left) - 'a']--;
                left++;
            }
        }
        return list;
    }
    private boolean check(int[] hash1, int[] hash2) {
        for (int i = 0; i < 26; i++) {
            if (hash1[i] != hash2[i]) return false;
        }
        return true;
    }
}